
#include "Store_menu.h"
#include <string>
#include <iomanip>
#include <iostream>


using namespace std;
using namespace Store;

string DisplayVCN::display_customers()
{	
	extern Store_menu m;
	int cart_id=0;
	int count=0;
	vector<int> topcusts;
	vector<int> freq;
	
	if(m.get_custs().size()==0||m.get_carts().size()==0||m.get_cart_items().size()==0)
	{
		string error = "No Data Found";
		return error;
	}
	for(int i=0;i<3;i++){
		freq.push_back(0);
		topcusts.push_back(0);
		}
	for(int i=0;i<m.get_custs().size(); i++) //go through custs
	{
		for(int j=0;j<m.get_carts().size();j++) //go through carts
		{
			if(m.get_carts().at(j).getCustId()==m.get_custs().at(i).getCustId()) //look for cart corresponding to cust
			{
				cart_id=m.get_carts().at(j).getCartId();
				for(int k=0;k<m.get_cart_items().size();k++) //fo through cart items
				{
					if(m.get_cart_items().at(k).getCartId()==cart_id) //look for cart item correspinding to cart
					{
						count++;
					}
				}
			}
		}
		
		if(freq[0]<count)
		{
			topcusts.insert(topcusts.begin(),m.get_custs().at(i).getCustId());
			freq.insert(freq.begin(),count);	
		}
		else if(freq[1]<count)
		{
			topcusts.insert(topcusts.begin()+1,m.get_custs().at(i).getCustId());
			freq.insert(freq.begin()+1,count);	
		}
		else if(freq[2]<count)
		{
			topcusts.insert(topcusts.begin()+2,m.get_custs().at(i).getCustId());
			freq.insert(freq.begin()+2,count);
		}
		count=0; //reset counter
	}
	stringstream sline (stringstream::in | stringstream::out);
	sline<<"The three most frequent customers are:\n\n";
	sline<<"CustID\tCustomer Name\n\n";
	for (int i=0;i<m.get_custs().size();i++)
	{
		if(m.get_custs().at(i).getCustId()==topcusts[0]||m.get_custs().at(i).getCustId()==topcusts[1]||m.get_custs().at(i).getCustId()==topcusts[2])
		{
			sline<<m.get_custs().at(i).getCustId()<<"\t";
			sline<<m.get_custs().at(i).getCustName()<<"\n";
		}
	}
	return sline.str();
}
